﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.Linq.Expressions;

namespace CGOnline.Server.Core
{
    public class XmlAccessRestrictions:AccessRestrictions
    {
        XDocument _xml;
        public XmlAccessRestrictions(string xmlfilepath)
        {
            _xml = XDocument.Load(xmlfilepath);
            if (_xml == null)
                throw new Exception(string.Format("Can not load config xml({0})",xmlfilepath));
        }
        public override bool CheckAccess(StateObject targetobject, string methodname, AccessType accesstype)
        {
            //XNamespace ns = "";
            var restriction = from sr in _xml.Descendants("StateRestriction")
                     where (string)sr.Attribute("Type")==accesstype.ToString()&&
                     (string)sr.Attribute("Name")==methodname&&
                     (string)sr.Parent.Parent.Attribute("OwnerType")==targetobject.GetType().Name
                    select (string)sr.Attribute("Restriction");
            if (restriction.Count() > 0)
            {
                var param = Expression.Parameter(typeof(Dictionary<string, string>), "CurrentStateGroup");
                var rs = restriction.First().Split(' ');
            }
            //underconstruct
            throw new NotImplementedException();
        }
    }
}
